package com.start.system.user.controller;

import com.start.admin.service.StartSysUserRoleService;
import com.start.common.api.Result;
import com.start.common.details.BaseUserDetail;
import com.start.security.StartTokenTools;
import com.start.security.StartUserPermissionTools;
import com.start.security.annotations.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/role")
public class UserRoleController {

    @Autowired
    private StartSysUserRoleService userRoleService;

    @Autowired
    private StartUserPermissionTools userPermissionTools;

    @GetMapping("/getUserRoles")
    @DataPermission("views/sys/role/index")
    public Result<?> getUserRoles(String userId) {
        if (StringUtils.isNotEmpty(userId)) {
            return Result.ok(userRoleService.getRoleListByUserId(userId));
        }
        BaseUserDetail userDetails = StartTokenTools.getCurrentUser();
        return Result.ok(userPermissionTools.getUserRole(userDetails));
    }

    @PostMapping("/saveUserRoles/{userId}")
    public Result<?> submit(@PathVariable("userId") String userId, @RequestBody String[] roleIds) {
        userRoleService.saveRoleUser(userId, roleIds);
        return Result.ok();
    }

}
